package com.right.health.app.widget.banner;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;

/**
 * 允许禁止滑动的ViewPager
 * Created by Supreme on 16/2/24.
 */
public class CanScrollViewPager extends ViewPager {

    private boolean isCanScroll = true;

    public CanScrollViewPager(Context context) {
        super(context);
    }

    public CanScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 设置是否允许滚动
     * @param isCanScroll true false
     */
    public void setCanScroll(boolean isCanScroll){
        this.isCanScroll = isCanScroll;
    }

    @Override
    public void setCurrentItem(int item) {
        boolean isCanScroll = this.isCanScroll;
        this.isCanScroll = true;

        super.setCurrentItem(item);

        this.isCanScroll = isCanScroll;
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        boolean isCanScroll = this.isCanScroll;
        this.isCanScroll = true;

        super.setCurrentItem(item, smoothScroll);

        this.isCanScroll = isCanScroll;
    }

    @Override
    public void scrollTo(int x, int y){
        if (isCanScroll){
            super.scrollTo(x, y);
        }
    }
}
